/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/KeyExchange/cryptoVerifier.cpp,v 1.3 2003/11/21 12:39:24 itsme Exp $
 *
 * this module performs several tests, to ensure all security facilities 
 * are functioning correctly:
 *    - the symmetric crypto
 *    - the diffiehellman primes
 *    - the secure hash
 *    - the secure memory erasure
 *
 */

#include "debug.h"

#include "fnaBlockCypher.h"
#include "fnaHashFunction.h"
#include "cryptoTwoFish.h"
#include "bignum.h"
#include "CryptoVerifier.h"

#include "PrimalityTest.h"

#include "stringutils.h"

CryptoVerifier::CryptoVerifier(const Number& modulusprime, const Number& subgroupprime, const Number& generator)
: m_modulus_prime(modulusprime), m_subgroup_prime(subgroupprime), m_subgroup_generator(generator)
{
}
CryptoVerifier::~CryptoVerifier()
{
}

// this thread will run for about 3 minutes on the xda 206 Mhz ARM.
DWORD CryptoVerifier::ThreadProc()
{

    SetThreadPriority(Handle(), THREAD_PRIORITY_LOWEST);

    DWORD t0= GetTickCount();
	bool a = false/*!VerifyPrimes()*/;
    if (a
            || !VerifySHA256()
            || !VerifyTwoFish()
            || !VerifyAES()
            || !VerifyMemoryErasure())
    {
        ErrorDetectedNotification();
        return 1;
    }

    DWORD t1= GetTickCount();
    debug("crypto functions tested OK in %d msec\n", t1-t0 );
    return 0;
}

//-----------------------------------------------------------------------------

bool CryptoVerifier::VerifyPrimes()
{
    Number x= m_subgroup_generator.ModularPower(m_subgroup_prime, m_modulus_prime);

    return x!=1 && IsPrime(m_modulus_prime) && IsPrime(m_subgroup_prime);
}

//-----------------------------------------------------------------------------

// todo: get test vectors from external source ( these were just calculated by me )

#define N_AESTESTS  32
static BYTE aeskeys[N_AESTESTS][32]= {
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, },
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F2,0xBE, },
{ 0x3A,0xD5,0x6A,0x33,0xCE,0x89,0xD8,0xD3,0x51,0x0C,0x41,0x52,0xDE,0xE7,0x34,0x12,0x6F,0x52,0x27,0x2E,0x61,0x72,0x56,0x88,0x5E,0xC2,0xE4,0xD2,0xDE,0xF7,0x2A,0xD2, },
{ 0x13,0xED,0x08,0x13,0x7C,0x66,0xC2,0x00,0x0D,0x3F,0xE9,0xE1,0x67,0x38,0x78,0x64,0xDA,0x1D,0xAA,0xBA,0x22,0x73,0xFE,0x11,0xFC,0x6A,0x60,0x92,0x20,0x7D,0x9C,0xE3, },
{ 0x3C,0x2C,0x53,0xCC,0xDB,0x47,0x21,0xF4,0x4B,0x6C,0x05,0x7B,0xF7,0x58,0x36,0x1A,0xF9,0xA9,0x15,0x29,0x49,0x80,0x7D,0x23,0xA3,0x67,0xCF,0x97,0x68,0x9E,0xB4,0xB2, },
{ 0x95,0xE7,0x44,0xB6,0x60,0x44,0x52,0x9E,0x2D,0xC2,0xB0,0x6B,0xF8,0x4F,0xF3,0xD6,0x72,0xE0,0x91,0xEF,0xBA,0x1C,0x02,0xF6,0xE0,0xD3,0x67,0xF5,0xF6,0x32,0xD3,0xF2, },
};

static BYTE aesvalues[N_AESTESTS][16]= {
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, },
{ 0x8B,0x79,0xEE,0xCC,0x93,0xA0,0xEE,0x5D,0xFF,0x30,0xB4,0xEA,0x21,0x63,0x6D,0xA4, },
{ 0xC7,0x37,0x31,0x7F,0xE0,0x84,0x6F,0x13,0x2B,0x23,0xC8,0xC2,0xA6,0x72,0xCE,0x22, },
{ 0xE5,0x8B,0x82,0xBF,0xBA,0x53,0xC0,0x04,0x0D,0xC6,0x10,0xC6,0x42,0x12,0x11,0x68, },
{ 0x10,0xB2,0x96,0xAB,0xB4,0x05,0x04,0x99,0x5D,0xB7,0x1D,0xDA,0x0B,0x7E,0x26,0xFB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 0x87, 0x0c, 0x73, 0x07, 0xfb, 0x06, 0xdf, 0xb2, 0x39, 0xae, 0xa6, 0xcf, 0xb0, 0x72},
{ 0x20, 0x76, 0xf7, 0x05, 0xea, 0x36, 0x62, 0x2e, 0x7b, 0x75, 0xa6, 0x0b, 0x68, 0xe4, 0x5b, 0xcb},
{ 0x3a, 0xc9, 0x18, 0x9d, 0x80, 0x80, 0x9e, 0xe9, 0x3d, 0x9e, 0xcc, 0x21, 0x48, 0x8e, 0xae, 0xd9},
{ 0x03, 0x2a, 0xe9, 0xe9, 0x48, 0x37, 0x79, 0xfe, 0x5c, 0x80, 0x7b, 0x8a, 0x83, 0xd2, 0x56, 0x78},
{ 0xe5, 0x22, 0x6e, 0x6d, 0xce, 0xbb, 0xbd, 0x5e, 0x9f, 0xf4, 0x0d, 0xca, 0x77, 0xc1, 0x33, 0x4b},
{ 0xf1, 0x4f, 0x41, 0x6c, 0x65, 0x8a, 0x98, 0xfb, 0x64, 0xed, 0xc2, 0x15, 0x32, 0x8a, 0x77, 0x02},
{ 0x8e, 0xe9, 0x26, 0x04, 0xb6, 0x0c, 0x30, 0x22, 0xc5, 0xac, 0x32, 0xde, 0xab, 0x90, 0x19, 0xe5},
{ 0xff, 0x6b, 0xce, 0x7b, 0xd0, 0x52, 0xcb, 0xf5, 0x14, 0x79, 0x2a, 0x40, 0x0a, 0xcf, 0xe3, 0x00},
{ 0x8a, 0x66, 0xce, 0x33, 0x3d, 0x20, 0xed, 0xbb, 0xe1, 0x36, 0x03, 0xca, 0xc9, 0x57, 0xfa, 0xb5},
{ 0x28, 0x83, 0x69, 0x06, 0x07, 0x08, 0xb4, 0x70, 0x2a, 0xcc, 0x66, 0xc0, 0xba, 0x0b, 0x64, 0xad},
{ 0x67, 0xef, 0x11, 0x98, 0xf4, 0x27, 0x4f, 0x5a, 0xe9, 0xf6, 0x59, 0x53, 0x38, 0x8e, 0xff, 0x06},

};


// http://csrc.nist.gov/encryption/aes/rijndael/
bool CryptoVerifier::VerifyAES()
{
    BlockCypher aes;

    for (int i=0 ; i<N_AESTESTS ; i++)
    {
        aes.SetKey(ByteVector(aeskeys[i], aeskeys[i]+BlockCypher::KEYSIZE));

        ByteVector result= aes.Encrypt(ByteVector(aesvalues[i], aesvalues[i]+BlockCypher::BLOCKSIZE));

        if (result!=ByteVector(aescrypts[i], aescrypts[i]+BlockCypher::BLOCKSIZE))
        {
            debug("ERROR in AES test %d: %hs\n", i, hexdump(result).c_str());
            return false;
        }
    }

    return true;
}

//-----------------------------------------------------------------------------

#define SHA256HashSize 32

#define N_SHATESTS 3

#define SHATEST1   "abc"
#define SHATEST2   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
#define SHATEST3   "a"

static char *shatestarray[N_SHATESTS] =
{
    SHATEST1,
    SHATEST2,
    SHATEST3
};
static long int sharepeatcount[N_SHATESTS] = { 1, 1, 1000000 };
static BYTE sharesultarray[N_SHATESTS][SHA256HashSize] =
{
{ 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 
  0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad },

{ 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
  0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
  0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 
  0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 },

{ 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
  0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
  0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e, 
  0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0 },

};

// http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf
bool CryptoVerifier::VerifySHA256()
{
    HashFunction sha;
    int i, j;

    /*
     *  Perform SHA-256 tests
     */
    for(j = 0; j < N_SHATESTS; ++j)
    {
        sha.Reset();

        for(i = 0; i < sharepeatcount[j]; ++i)
        {
            sha.Add(ByteVector((BYTE*) shatestarray[j], (BYTE*)shatestarray[j]+strlen(shatestarray[j])));
        }

        ByteVector result= sha.GetResult();

        if (result!=ByteVector(sharesultarray[j], sharesultarray[j]+SHA256HashSize))
        {
            debug("ERROR in sha test %d: %hs\n", hexdump(result).c_str());
            return false;
        }
    }

    return true;
}

//-----------------------------------------------------------------------------
#define N_TFTESTS  12
static BYTE tfkeys[N_TFTESTS][32]= {
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, },
{ 0x57,0xFF,0x73,0x9D,0x4D,0xC9,0x2C,0x1B,0xD7,0xFC,0x01,0x70,0x0C,0xC8,0x21,0x6F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, },
{ 0xD4,0x3B,0xB7,0x55,0x6E,0xA3,0x2E,0x46,0xF2,0xA2,0x82,0xB7,0xD4,0x5B,0x4E,0x0D,0x57,0xFF,0x73,0x9D,0x4D,0xC9,0x2C,0x1B,0xD7,0xFC,0x01,0x70,0x0C,0xC8,0x21,0x6F, },
{ 0x90,0xAF,0xE9,0x1B,0xB2,0x88,0x54,0x4F,0x2C,0x32,0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0x8A,0x7F,0x35,0x28,0xB1,0x68,0xAC,0xFD,0xD1,0x38,0x6E,0x3F,0x51,0xE3,0x0C,0x2E,0x21,0x58,0xBC,0x3E,0x5F,0xC7,0x14,0xC1,0xEE,0xEC,0xA0,0xEA,0x69,0x6D,0x48, },
};

static BYTE tfvalues[N_TFTESTS][16]= {
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, },
{ 0x57,0xFF,0x73,0x9D,0x4D,0xC9,0x2C,0x1B,0xD7,0xFC,0x01,0x70,0x0C,0xC8,0x21,0x6F, },
{ 0xD4,0x3B,0xB7,0x55,0x6E,0xA3,0x2E,0x46,0xF2,0xA2,0x82,0xB7,0xD4,0x5B,0x4E,0x0D, },
{ 0x90,0xAF,0xE9,0x1B,0xB2,0x88,0x54,0x4F,0x2C,0x32,0xDC,0x23,0x9B,0x26,0x35,0xE6, },
{ 0x6C,0xB4,0x56,0x1C,0x40,0xBF,0x0A,0x97,0x05,0x93,0x1C,0xB6,0xD4,0x08,0xE7,0xFA, },
{ 0x30,0x59,0xD6,0xD6,0x17,0x53,0xB9,0x58,0xD9,0x2F,0x47,0x81,0xC8,0x64,0x0E,0x58, },
{ 0xE6,0x94,0x65,0x77,0x05,0x05,0xD7,0xF8,0x0E,0xF6,0x8C,0xA3,0x8A,0xB3,0xA3,0xD6, },
{ 0x5A,0xB6,0x7A,0x5F,0x85,0x39,0xA4,0xA5,0xFD,0x9F,0x03,0x73,0xBA,0x46,0x34,0x66, },
{ 0xDC,0x09,0x6B,0xCD,0x99,0xFC,0x72,0xF7,0x99,0x36,0xD4,0xC7,0x48,0xE7,0x5A,0xF7, },
{ 0xC5,0xA3,0xE7,0xCE,0xE0,0xF1,0xB7,0x26,0x05,0x28,0xA6,0x8F,0xB4,0xEA,0x05,0xF2, },
{ 0x24,0x8A,0x7F,0x35,0x28,0xB1,0x68,0xAC,0xFD,0xD1,0x38,0x6E,0x3F,0x51,0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0x75,0xF5,0xDD,0xF4,0xC3,0x3B,0x97,0xA2,0x05, },
};

// http://www.counterpane.com/twofish.pdf
bool CryptoVerifier::VerifyTwoFish()
{
    CryptoTwoFish  two;

    for (int i=0 ; i<N_TFTESTS ; i++)
    {
        two.SetKey(ByteVector(tfkeys[i], tfkeys[i]+CryptoTwoFish::KEYSIZE));

        ByteVector result= two.Encrypt(ByteVector(tfvalues[i], tfvalues[i]+CryptoTwoFish::BLOCKSIZE));

        if (result!=ByteVector(tfcrypts[i], tfcrypts[i]+CryptoTwoFish::BLOCKSIZE))
        {
            debug("ERROR in 2fish test %d: %hs\n", hexdump(result).c_str());
            return false;
        }
    }

    return true;
}

void CryptoVerifier::ErrorDetectedNotification()
{
    MessageBox(0, L"ERROR IN CRYPTO\nPLEASE REINSTALL!!", L"ERROR", 0);
    TerminateProcess(GetCurrentProcess(), 1);
}

BYTE *TestByteVectorFreedMemory()
{
    BYTE *p;
    ByteVector *bv = new ByteVector();
    for (int i=0 ; i<256 ; i++)
        bv->push_back(i);

    p= vectorptr(*bv);

    delete bv;

    return p;
}
// 
bool CryptoVerifier::VerifyMemoryErasure()
{
    BYTE *p= TestByteVectorFreedMemory();

    debug("bv=%hs\n", hexdump(p, 256).c_str());

    return true;
}
